#include <bits/stdc++.h>
using namespace std;
#define IOS() ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define int long long
#define pb(x) push_back(x)
#define mp make_pair
#define fir first
#define se second
int a1[200005],a[200005],b[200005];
int ans[200005];
string s[200005];
struct ope{
    int c,id;
}q[200005];
bool cmp(ope x,ope y){
    return x.c<y.c;
}
int lg[200005];
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    IOS();
    for(int i=2;i<=200000;i++)
        lg[i]=lg[i>>1]+1;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a1[i];
    for(int i=1;i<=m;i++){
        int x;
        cin>>x;
        ope k;
        k.c=x,k.id=i;
        q[i]=k;
    }
    sort(q+1,q+m+1,cmp);
    int mx=lg[n];
    if((1ll<<mx)<n)
        mx++;
    // cout<<mx<<"\n";
    for(int i=1;i<=mx;i++){
        cin>>s[i];
        s[i]=" "+s[i];
    }
    int t;
    cin>>t;
    while(t--){
        int x[4];
        for(int i=0;i<4;i++)
            cin>>x[i];
        for(int i=1;i<=n;i++)
            b[i]=a1[i]^(x[i%4]),ans[i]=0;
        // for(int i=1;i<=n;i++){
        //     cout<<a[i]<<" ";
        // }
        // cout<<"\n";
        for(int i=1;i<=m;i++){
            if(q[i].c==q[i-1].c){
                ans[q[i].id]=ans[q[i-1].id];
                continue;
            }
            for(int j=1;j<=n;j++)
                a[j]=j;
            int k=lg[q[i].c];
            // cout<<k<<"\n";
            for(int j=1;j<=k;j++){
                for(int l=1;l<=1<<k-j;l++)
                    a[l]=(s[j][l]=='0'?(b[a[(l-1)*2+1]]>=j?a[(l-1)*2+1]:a[l*2]):(b[a[l*2]]>=j?a[l*2]:a[(l-1)*2+1]));
            }
            ans[q[i].id]=a[1];
        }
        // cout<<ans[2]<<"\n";
        int res=0;
        for(int i=1;i<=n;i++)
            res^=i*ans[i];
        cout<<res<<"\n";
    }
    return 0;
}